﻿
@{
    ViewBag.Title = "模板编辑";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
@model IOA.MES.DataAccess.VModel.VLabelTemplate

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.TemplateName}}</label>
                    <div class="layui-input-inline">
                        <input type="text" name="TemplateName" autocomplete="off" class="layui-input" value="@(Model?.TemplateName)">
                    </div>
                    <div class="layui-input-inline">
                        <input name="PkId" value="@(Model?.PkId??0)" type="hidden" />
                        <button class="layui-btn layuiadmin-btn-list layui-btn-normal" lay-submit lay-filter="submit">
                            <i class="layui-icon layui-icon-ok layuiadmin-button-btn"></i>{{lang.Field.Submit}}
                        </button>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.SerialNumber}}</label>
                    <div class="layui-input-inline">
                        <input type="text" name="SerialNum" readonly autocomplete="off" class="layui-input" value="@(Model==null?string.Empty:Model.SerialNum)">
                    </div>
                    <button type="button" class="layui-btn layuiadmin-btn-list btnupload" id="btnSerialNum">
                        <i class="layui-icon">&#xe67c;</i>{{lang.Field.SelectTemplateFile}}
                    </button>
                    <button type="button" class="layui-btn layuiadmin-btn-list layui-btn-danger btnDel" name="SerialNum"><i class="layui-icon">&#xe640;</i></button>
                    <a class="layui-btn layuiadmin-btn-list layui-btn-normal" target="_blank" href="/Common/Download?url=@(HttpUtility.UrlEncode(Model?.SerialNum))"><i class="layui-icon">&#xe601;</i></a>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.BorderBarCode}}</label>
                    <div class="layui-input-inline">
                        <input type="text" name="BorderBarCode" readonly autocomplete="off" class="layui-input" value="@(Model==null?string.Empty:Model.BorderBarCode)">
                    </div>
                    <button type="button" class="layui-btn layuiadmin-btn-list btnupload" id="btnBorderBarCode">
                        <i class="layui-icon">&#xe67c;</i>{{lang.Field.SelectTemplateFile}}
                    </button>
                    <button type="button" class="layui-btn layuiadmin-btn-list layui-btn-danger btnDel" name="BorderBarCode"><i class="layui-icon">&#xe640;</i></button>
                    <a class="layui-btn layuiadmin-btn-list layui-btn-normal" target="_blank" href="/Common/Download?url=@(HttpUtility.UrlEncode(Model?.BorderBarCode))"><i class="layui-icon">&#xe601;</i></a>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.Nameplate}}</label>
                    <div class="layui-input-inline">
                        <input type="text" name="Nameplate" readonly autocomplete="off" class="layui-input" value="@(Model==null?string.Empty:Model.Nameplate)">
                    </div>
                    <button type="button" class="layui-btn layuiadmin-btn-list btnupload" id="btnNameplate">
                        <i class="layui-icon">&#xe67c;</i>{{lang.Field.SelectTemplateFile}}
                    </button>
                    <button type="button" class="layui-btn layuiadmin-btn-list layui-btn-danger btnDel" name="Nameplate"><i class="layui-icon">&#xe640;</i></button>
                    <a class="layui-btn layuiadmin-btn-list layui-btn-normal" target="_blank" href="/Common/Download?url=@(HttpUtility.UrlEncode(Model?.Nameplate))"><i class="layui-icon">&#xe601;</i></a>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.CurrentLabel}}</label>
                    <div class="layui-input-inline">
                        <input type="text" name="CurrentTag" readonly autocomplete="off" class="layui-input" value="@(Model==null?string.Empty:Model.CurrentTag)">
                    </div>
                    <button type="button" class="layui-btn layuiadmin-btn-list btnupload" id="btnCurrentTag">
                        <i class="layui-icon">&#xe67c;</i>{{lang.Field.SelectTemplateFile}}
                    </button>
                    <button type="button" class="layui-btn layuiadmin-btn-list layui-btn-danger btnDel" name="CurrentTag"><i class="layui-icon">&#xe640;</i></button>
                    <a class="layui-btn layuiadmin-btn-list layui-btn-normal" target="_blank" href="/Common/Download?url=@(HttpUtility.UrlEncode(Model?.CurrentTag))"><i class="layui-icon">&#xe601;</i></a>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.EnergyLabel}}</label>
                    <div class="layui-input-inline">
                        <input type="text" name="EnergyTag" readonly autocomplete="off" class="layui-input" value="@(Model==null?string.Empty:Model.EnergyTag)">
                    </div>
                    <button type="button" class="layui-btn layuiadmin-btn-list btnupload" id="btnEnergyTag">
                        <i class="layui-icon">&#xe67c;</i>{{lang.Field.SelectTemplateFile}}
                    </button>
                    <button type="button" class="layui-btn layuiadmin-btn-list layui-btn-danger btnDel" name="EnergyTag"><i class="layui-icon">&#xe640;</i></button>
                    <a class="layui-btn layuiadmin-btn-list layui-btn-normal" target="_blank" href="/Common/Download?url=@(HttpUtility.UrlEncode(Model?.EnergyTag))"><i class="layui-icon">&#xe601;</i></a>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.Mark}}</label>
                    <div class="layui-input-inline">
                        <input type="text" name="Mark" readonly autocomplete="off" class="layui-input" value="@(Model==null?string.Empty:Model.Mark)">
                    </div>
                    <button type="button" class="layui-btn layuiadmin-btn-list btnupload" id="btnMark">
                        <i class="layui-icon">&#xe67c;</i>{{lang.Field.SelectTemplateFile}}
                    </button>
                    <button type="button" class="layui-btn layuiadmin-btn-list layui-btn-danger btnDel" name="Mark"><i class="layui-icon">&#xe640;</i></button>
                    <a class="layui-btn layuiadmin-btn-list layui-btn-normal" target="_blank" href="/Common/Download?url=@(HttpUtility.UrlEncode(Model?.Mark))"><i class="layui-icon">&#xe601;</i></a>
                </div>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['form', 'layer', 'upload'], function () {
                var layer = layui.layer,
                    upload = layui.upload,
                    form = layui.form;

                initUpload("btnSerialNum");
                initUpload("btnBorderBarCode");
                initUpload("btnNameplate");
                initUpload("btnCurrentTag");
                initUpload("btnEnergyTag");
                initUpload("btnMark");
                function initUpload(btnId) {
                    upload.render({
                        elem: '#' + btnId //绑定元素
                        , accept: "file"
                        , exts: 'csv|btw|xls|xlsm'
                        , url: '/Common/Upload?fileName='
                        , before: function (obj) {
                            var files = obj.pushFile();
                            var file = "";
                            for (var item in files) {
                                file = files[item];
                            }
                            var ext = file.name.substring(file.name.lastIndexOf("."), file.name.length);
                            this.url = '/Common/Upload?fileName=' + encodeURIComponent("/labels/" + (new Date()).valueOf() + ext)
                            layer.load();
                        }
                        , done: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg);
                                return;
                            }
                            var inputName = this.item[0].id.replace("btn", "");
                            $("input[name=" + inputName + "]").val(res.Data.AbsolutePath);
                            layer.closeAll();
                        }
                        , error: function (res) {
                            layer.msg("上传失败");
                            layer.closeAll();
                        }
                    });
                }

                $(document).on("click", ".btnDel", function () {
                    $("input[name=" + $(this).attr("name") + "]").val("");
                });

                form.on('submit(submit)', function (data) {
                    var forms = data.field;
                    console.log(forms);
                    if (forms.SerialNum.trim() == '') {
                        layer.msg("请上传序列号", { icon: 5 });
                        return;
                    } else if (forms.BorderBarCode.trim() == '') {
                        layer.msg("请上传边框条码", { icon: 5 });
                        return;
                    } else if (forms.Nameplate.trim() == '') {
                        layer.msg("请上传铭牌", { icon: 5 });
                        return;
                    } else if (forms.Mark.trim() == '') {
                        layer.msg("请上传唛头", { icon: 5 });
                        return;
                    }
                    var loadId = layer.load();
                    $.ajax({
                        url: "/ParaSetting/SaveLabelTemplate",
                        data: {
                            model: data.field
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            layer.msg("保存成功！", { icon: 6, time: 1500 });
                        }
                    });
                    return false;
                });
            });
        }
    </script>
}
